/**
 * Copyright ©2016-2022 tianyu education Corporation, All Rights Reserved
 * http://www.whty.com.cn/
 */
package profile.hujia.redis.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import profile.hujia.redis.dao.model.JedisCommonConfigAttribute;
import profile.hujia.redis.dao.model.JedisPoolConfigAttribute;
import profile.hujia.redis.manager.JedisPoolCache;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * class info
 *
 * @author hujia
 * @date 2019-12-24 08:49
 */
@Configuration
public class TestJedisPoolConfig {

    @Bean
    public JedisPoolCache jedisPoolCache(JedisPoolConfigAttribute attribute, JedisCommonConfigAttribute common) {
        JedisPoolCache jedisPoolCache = new JedisPoolCache();
        jedisPoolCache.setJedisPool(redisPoolFactory(attribute,common));
        return jedisPoolCache;
    }

    @Bean
    public JedisPool redisPoolFactory(JedisPoolConfigAttribute attribute,JedisCommonConfigAttribute common) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(common.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(common.getMaxWaitMillis());
        jedisPoolConfig.setMinIdle(common.getMinIdle());
        jedisPoolConfig.setMaxTotal(common.getMaxTotal());
        int timeout = common.getTimeout();
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, attribute.getIp(), attribute.getPort(), timeout,
                attribute.getPassword());
        return jedisPool;
    }

}